import axios, { AxiosResponse, AxiosError, FormData } from '@ohos/axios'
import { Param } from '../common/Param'

interface body {
  CLASS?: string,
  class?: string,
  passMD5?: string,
  type?: number,
  imagePath?: string,
  detectResultSize?: number,
  fireConfirmed?: number,
  className?: string,
  operate?: string,
  name?: string,
  id?: number,
}


/**
 * @desc 获取能耗本周数据
 * @param {data} post请求的数据
 * @param {params} get请求的数据
 *
 */
export async function getWeather(type: string) {
  return new Promise((resolve,reject)=>{
    axios.get<string, AxiosResponse<string>, null>('https://restapi.amap.com/v3/weather/weatherInfo', { params: { key: "9130eae9faeea02a6890ca0b746feb57", city: '430112', extensions: type } })
      .then((response: AxiosResponse) => {
        console.info("getWeather result:" + JSON.stringify(response.data));
        if(type == 'all') {
          resolve(response.data.forecasts[0])
        } else {
          resolve(response.data.lives[0])
        }
      })
      .catch((error: AxiosError) => {
        reject(error.message)
        console.error("getWeather error result:" + error.message);
      });
  })
}

/**
 * @desc 修改门禁密码
 * @param {data} post请求的数据
 * @param {params} get请求的数据
 *
 */
export async function modifyPwd(pwd: string) {
  return new Promise((resolve,reject)=>{
    axios.post<string, AxiosResponse<any>, body>('sqlDoorPassMod', {
      class: Param.classRoomNumber,
      passMD5: pwd
    })
      .then((response: AxiosResponse<any>) => {
        const ret = response.data.data
        resolve(ret)
      })
      .catch((error) => {
        console.info(JSON.stringify(error));
        reject(error)
      });
  })
}
/**
 * @desc 校验门禁密码
 * @param {data} post请求的数据
 * @param {params} get请求的数据
 *
 */
export async function checkDoorPwd(pwd: string) {
  return new Promise((resolve,reject)=>{
    axios.post<string, AxiosResponse<any>, body>('sqlDoorPassCheck', {
      class: Param.classRoomNumber,
      passMD5: pwd
    })
      .then((response: AxiosResponse<any>) => {
        const ret = response.data.data[0].has_permission
        resolve(ret)
      })
      .catch((error) => {
        console.info(JSON.stringify(error));
        reject(error)
      });
  })
}
/**
 * @desc 获取能耗本年数据
 * @param {data} post请求的数据
 * @param {params} get请求的数据
 *
 */
export async function checkAdminPass(pwd: string) {
  return new Promise((resolve,reject)=>{
    axios.post<string, AxiosResponse<any>, body>('sqlAdminPassCheck', {
      passMD5: pwd
    })
      .then((response: AxiosResponse<any>) => {
        const ret = response.data.data[0].has_permission
        resolve(ret)
      })
      .catch((error) => {
        console.info(JSON.stringify(error));
        reject(error)
      });
  })
}

/**
 * @desc 人脸识别数据对比-开门
 * @param {data} post请求的数据
 * @param {params} get请求的数据
 *
 */
export async function faceRecognitionAccessControl(operate: number, imagePath: string, name?: string) {
  return new Promise((resolve,reject)=>{
    axios.post<string, AxiosResponse<any>, body>('http://127.0.0.1:5050/faceAddOrCheck', {
      className: Param.classRoomNumber,
      imagePath: imagePath,
      operate: `${operate}`,
      name: name ? name : ''
    })
      .then((response: AxiosResponse<any>) => {
        const ret = response.data
        console.log('daihai faceRecognitionAccessControl ret: ' + JSON.stringify(ret))
        resolve(ret)
      })
      .catch((error) => {
        console.info('daihai' + JSON.stringify(error));
        reject(error)
      });
  })
}

/**
 * @desc 获取开门人脸列表
 * @param {data} post请求的数据
 * @param {params} get请求的数据
 *
 */
export async function getFaceList() {
  return new Promise((resolve,reject)=>{
    axios.post<string, AxiosResponse<any>, body>('faceSelect', {
      className: Param.classRoomNumber
    })
      .then((response: AxiosResponse<any>) => {
        resolve(response.data.data)
      })
      .catch((error) => {
        console.info(JSON.stringify(error));
        reject(error)
      });
  })
}

/**
 * @desc 删除管理员面容数据
 * @param {data} post请求的数据
 * @param {params} get请求的数据
 *
 */
export async function deleteFace(faceId: number) {
  return new Promise((resolve,reject)=>{
    axios.post<string, AxiosResponse<any>, body>('faceDel', {
      CLASS: Param.classRoomNumber,
      id: faceId
    })
      .then((response: AxiosResponse<any>) => {
        resolve(response.data.data)
      })
      .catch((error) => {
        console.info(JSON.stringify(error));
        reject(error)
      });
  })
}
